package com.ez2find.backing;

import com.ez2find.manager.ForumManagerLocal;
import com.ez2find.manager.NickManagerLocal;
import com.ez2find.manager.SubscriptionManagerLocal;
import com.ez2find.model.Subscription;
import com.ez2find.util.SessionContext;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

public class SubscriptionsBean {

    // <editor-fold defaultstate="collapsed" desc="Define variables">
    @EJB
    ForumManagerLocal ejbForums;
    @EJB
    SubscriptionManagerLocal ejbSubscriptions;
    private Subscription subscription;
    private boolean newSubscription;
    private Long forumId;
// </editor-fold>

    public SubscriptionsBean() {
    }

    public boolean subscriptionExists(Long id) {
        SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
        boolean ans = false;
        for (Subscription s : sessionBean.getEjbSession().getOwner().getSubscriptions()) {
            if (s.getForum().getId().equals(id)) {
                ans = true;
            }
        }
        return ans;
    }

    public String saveSubscription() {
        SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");

        System.out.println("Saving changes..");
        if (subscriptionExists(forumId)) {
            FacesMessage msg = new FacesMessage("Forum's subscription already created.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        }
        try {
            if ((getSubscription() != null) && (getSubscription().getId() != null)) {
                subscription.setForum(ejbForums.find(forumId));
                ejbSubscriptions.update(subscription);
                sessionBean.getEjbSession().getOwner().getSubscriptions().set(sessionBean.getEjbSession().getOwner().getSubscriptions().indexOf(subscription), subscription);
                System.out.println("Subscription updated correctly.");
                FacesMessage msg = new FacesMessage("Subscription updated correctly.");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return "success";
            } else {
                subscription.setForum(ejbForums.find(forumId));
                subscription.setUser(sessionBean.getEjbSession().getOwner());
                ejbSubscriptions.create(subscription);
                sessionBean.getEjbSession().getOwner().getSubscriptions().add(subscription);
                System.out.println("Subscription created correctly.");
                FacesMessage msg = new FacesMessage("Subscription created correctly.");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return "success";
            }

        } catch (Exception ex) {
            FacesMessage msg = new FacesMessage("Error creating/updating subscription.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            Logger.getLogger(SubscriptionsBean.class.getName()).log(Level.SEVERE, null, ex);
        return "persistanceError";    
        }
        
    }

    public String removeSubscription() {
        try {
            SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
            ejbSubscriptions.delete(subscription);
            sessionBean.getEjbSession().getOwner().getSubscriptions().remove(subscription);
            System.out.println("Subscription removed");
            FacesMessage msg = new FacesMessage("Subscription removed.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        } catch (Exception ex) {
            Logger.getLogger(NickManagerLocal.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public List<Subscription> getSubscriptions() {
        return ejbSubscriptions.listAll();
    }

    // <editor-fold defaultstate="collapsed" desc="Setters and getters"> 
    public Subscription getSubscription() {
        if (subscription == null) {
            subscription = new Subscription();
        }
        return subscription;
    }

    public void setSubscription(Subscription subscription) {
        this.subscription = subscription;
    }

    public boolean isNewSubscription() {
        return newSubscription;
    }

    public void setNewSubscription(boolean newSubscription) {
        this.newSubscription = newSubscription;
    }

    public Long getForumId() {
        return forumId;
    }

    public void setForumId(Long forumId) {
        this.forumId = forumId;
    }

// </editor-fold>
}
